﻿using System.Configuration.Abstractions;
using SimpleInjector;
using System.Web.Http;
using Sustainalytics.ClientDW.Entities;
using Sustainalytics.Controversies.BusinessLogic;
using Sustainalytics.Claims;
using Sustainalytics.MongoDB.Extensions;
using Sustainalytics.Entities.ProductInvolvement;
using Sustainalytics.Synchronization.Validation.ValidationRules.Common;
using Sustainalytics.DataLayer;
using MongoDB.Driver;
using Sustainalytics.Entities.SustainableProducts;
using Sustainalytics.ClientDW.DataAccessLayer;

namespace Sustainalytics.Companies.Service
{
    public static class CompositionRoot
    {

        public static Container RegisterDependencies(HttpConfiguration config)
        {
            var container = new Container();
            var validationConnectionString = ConfigurationManager.Instance.ConnectionStrings["ValidationRules"].ConnectionString;
          
            container.RegisterMisc();

            container.RegisterCollections(validationConnectionString);

            container.RegisterRepositories();

            container.RegisterBussinessLogic();

            container.Verify();
            return container;
        }

        private static void RegisterCollections(this Container container, string validationConnectionString)
        {
            container.Register(() => MongoFactory.GetCollectionFromDatabase<CorporateData>(MongoFactory.GetConnectionStrings().ClientDW));
            container.Register(() => MongoFactory.GetCollectionFromDatabase<ProductInvolvementData>(MongoFactory.GetConnectionStrings().ProductInvolvementDW));
            container.Register(() => MongoFactory.GetCollectionFromDatabase<SustainableProductsData>(MongoFactory.GetConnectionStrings().ProductInvolvementDW));
            container.Register(() => MongoFactory.GetCollectionFromDatabase<WhiteList>(validationConnectionString));
        }
        private static void RegisterRepositories(this Container container)
        {
           container.Register(typeof(ICompaniesRepository), () => new CompaniesRepository(MongoFactory.GetConnectionStrings().ClientDW));
            container.Register(typeof(IUniversesRepository), () => UniversesRepository.GetUniversesRepository(MongoFactory.GetConnectionStrings().ClientUniverses));
        }

        private static void RegisterBussinessLogic(this Container container)
        {
            container.Register((typeof(ICompanyMatchingLogic)), () => new CompanyMatchingLogic(container.GetInstance<IMongoCollection<CorporateData>>()));
        }

        private static void RegisterMisc(this Container container)
        {
            container.Register(() => MongoFactory.GetConnectionStrings());
            container.RegisterSingleton(typeof(IClaimsChecker), () => new ClaimsChecker());
        }
    }
}
